/**
 * @file string.h
 * @author Toni Uhlig
 * @date 9 Oct 2014
 * @brief Defines data structure and algorithms for strings.
 */

#ifndef STRING_H
#define STRING_H 1

/* import string data structure */
#include "array.h"

#ifndef STR_INITSIZ
# define STR_INITSIZ 3
#endif

typedef struct array string_t; /**< A string is basically an array of char. */

/**
 * @brief Reserves the memory for the internal data structure (empty #string_t).
 * @return A Pointer to the an array struct.
 */
string_t *
create_empty_string(void);

/**
 * @brief Like #create_empty_string, but creates a non-empty string.
 * @param s A NULL-terminated string.
 * @return A Pointer to the an array struct.
 */
string_t *
create_string(const char *s);

/**
 * @brief Like #create_string, but creates a string_t from a (not) NULL-terminated string with specified length.
 * @param s A NULL-terminated string.
 * @param len The length of string s.
 * @return A Pointer to the an array struct.
 */
string_t *
create_nstring(const char *s, size_t len);

/**
 * @brief Creats a formatted string_t.
 * @param fmt format string
 * @return A Pointer to the resulting formatted-string_t.
 */
string_t *
create_fstring(const char *fmt, ...);

/**
 * @brief Creates an exact copy of a string_t.
 * @param src source
 * @return A Pointer to the duped string_t.
 */
string_t *
create_dupstring(string_t *src);

/**
 * @brief Free all memory related to a string_t.
 * @param str A string_t pointer.
 */
void
free_string(string_t *str);

/**
 * @brief Set a custom string. This function does all the memory mgmt stuff for you.
 * @param dst a string_t pointer
 * @param s A NULL-terminated string.
 */
void
set_string(string_t *dst, const char *s);

/**
 * @brief Like #set_string, but the user has to commit the length of the string.
 * @param dst a string_t pointer
 * @param s A (not) NULL-terminated string.
 * @param len The length of the string #s.
 */
void
set_nstring(string_t *dst, const char *s, size_t len);

/**
 * @brief Like #set_string, but accepts formatted strings.
 * @param dst a string_t pointer
 * @param fmt format string
 */
void
set_fstring(string_t *dst, const char *fmt, ...);

void
add_fstring(string_t *dst, const char *fmt, ...);

const char *
get_string(string_t *dst);

size_t
get_strlen(string_t *dst);

const char *
get_string_off(string_t *dst, size_t idx);

int
string_cmp(string_t *haystack, const char *needle, int start_idx);

void
add_string(string_t *dst, const char *s);

void
add_sstring(string_t *dst, string_t *src);

void
add_nstring(string_t *dst, const char *s, size_t len);

void
add_fstring(string_t *dst, const char *fmt, ...);

void
del_string(string_t *dst, size_t index, size_t len);

void
insert_string(string_t *dst, size_t index, const char *data, size_t data_len);

int
replace_string(string_t *dst, const char *needle, const char *replacer, size_t replen);

#ifdef ENABLE_DEBUG
void
print_string_debug(string_t *dst);
#endif

#endif
